home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 938 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  73 lines

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How best to handle constructor failure?
  5. Date: 8 Jan 1996 15:42:30 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4cre16$9oh@dawn.mmm.com>
  8. References: <4c36gl$5e5@nnrp1.news.primenet.com> <4cimnp$hne@news1.panix.com>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Jeffrey O. Katz (off@panix.com) wrote:
  13.  
  14. [snip]
  15.  
  16. >     Yes, using auto pointers will avoid memory leaks from un-destructed objects.
  17. > The problem, however, is how to alter the sequence of code existing OUTSIDE of
  18. > the constructor, based on the success or failure of the constructor.
  19.  
  20. >     In addition, we cannot use exceptions for a variety of reasons (e.g., some
  21. > functions must be exported from a DLL using a straight C or PASCAL calling
  22. > convention, and errors need to be handled by a calling application written in
  23. > another language), and, even if we could, it appears that we would still need
  24. > to use a longjmp() inside the try {...} in order to get out of the constructor so as
  25. > to be able to bypass the code that follows the constructor in the function.  Using
  26. > longjmp(), an initializer method that is called seperately from the constructor,
  27. > or various "test" methods (a method that returns a success/failure value) makes
  28. > code kludgy and inelegant.  I was wondering what the "correct" approach (if
  29. > there is such a thing) for handling the problem was.
  30.  
  31. > To make the problem a little clearer, what we need to do is something like this:
  32.  
  33. >   int MyFunction (int n, float y, ...)  {
  34. >     MyClass mc(n);
  35. >     int return_code;
  36. >     if (constructor failed entirely) {
  37. >         rc = STACK_OVERFLOW;
  38. >     } else if (allocations internal to the constructor failed) {
  39. >         rc = INSUFFICIENT_HEAP;
  40. >     } else {
  41. >         ... lots of code ...
  42. >         rc = SUCCESS;
  43. >     }
  44. >     return rc;
  45. >     // the implicitly called destructor will perform any needed cleanup
  46. >   }
  47.  
  48. In general, C++ code to be executed by another language must be completely
  49. wrapped in a try-block, with exceptions caught and reported in the manner
  50. appropriate to that language.  I don't see why any C++ code cannot be
  51. wrapped in that way.  For example:
  52.     int MyFunction (int n, float y, ...)  {
  53.     int rc = SUCCESS;
  54.     try {
  55.         MyClass mc(n);
  56.         ... lots of code ...
  57.       } catch (bad_alloc) {
  58.         rc = INSUFFICIENT_HEAP;
  59.     } catch (...) {
  60.         rc = SOME_OTHER_ERROR;
  61.       }
  62.       return rc;
  63.       // the implicitly called destructor will perform any needed cleanup
  64.     }
  65. --
  66. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  67. 3M Company                      phone:  (612) 737-4643
  68. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  69. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  70.     But 3M speaks for me -- I did not write the following line:
  71.  
  72. Opinions expressed herein are my own and may not represent those of 3M.
  73.